package self.hoy.demo.grainmall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html
 * @author hyh
 * @date 2020/7/9
 */
@Configuration
public class GrainElasticSearchConfiguration {

    @Value("${elasticsearch.host:127.0.0.1}")
    private String host;

    @Value("${elasticsearch.port:9200}")
    private Integer port;

    @Value("${elasticsearch.scheme:http}")
    private String scheme;

    public static final RequestOptions COMMON_OPTIONS;

    static {//建造者模式
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS = builder.build();
    }

    @Bean
    @Primary
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                    new HttpHost(host, port, scheme)));
        return client;

    }
}
